#!/bin/sh

CONFIG=/etc/kopano/server.cfg
test -f ${CONFIG} || exit 0

LOGPATH=$(grep -w client_update_log_path ${CONFIG} | grep -v \# | cut -d= -f2 | sed -e 's/[[:blank:]\r]//g')
if test -z "${LOGPATH}"; then
	LOGPATH=/var/lib/kopano/client
fi
if ! test -r "${LOGPATH}"; then
	exit 0
fi
LOGPATH=$(readlink -e ${LOGPATH})
if test -z "${LOGPATH}"; then
	exit 0
fi
PATHDEPTH=$(echo -en ${LOGPATH} | sed -e 's_[^/]__g' | wc -c)
if test -n ${LOGPATH} -a ${PATHDEPTH} -gt 2 -a -d ${LOGPATH}; then
	# remove all files older than 14 days
	find ${LOGPATH} -mindepth 2 -maxdepth 2 -type f -ctime +14 -exec rm {} \;
	# remove all empty directories
	find ${LOGPATH} -mindepth 1 -maxdepth 1 -type d -exec rmdir --ignore-fail-on-non-empty {} \;
fi
